package com.lukebang.controller;

import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lukebang.dao.CityCostRuleDAO;
import com.lukebang.dao.MeasureIndentDAO;
import com.lukebang.dao.MerchantDAO;
import com.lukebang.dao.MerchantsWalletRecordDAO;
import com.lukebang.dao.WorkerDao;
import com.lukebang.dao.WorkerWalletRecordDao;
import com.lukebang.pojo.Result;
import com.lukebang.service.IInstallIndentService;
import com.lukebang.service.IMeasureIndentService;

/**
 * 
 * MeasureIndentForController 上午10:50:09
 * 
 * Copyright zhaocj Inc. All rights reserved. Love ME Like Justin Bieber.
 */
@Controller
@RequestMapping("/indent")
public class MeasureIndentForController {
	private static Logger logger = LogManager.getLogger(MeasureIndentForController.class.getName());
	@Resource
	@Qualifier("measureIndentSER")
	private IMeasureIndentService measureIndentSER;
	@Resource
	private MerchantDAO  merchantDAO;
	@Resource
	private MerchantsWalletRecordDAO merchantsWalletRecordDAO;
	@Resource
	private WorkerDao workerDao;
	@Resource
	private WorkerWalletRecordDao workerWalletRecordDao;
	@Resource
	CityCostRuleDAO cityCostRuleDAO;
	@Resource
	MeasureIndentDAO measureIndentDAO;

	// 添加测量订单
	@RequestMapping("/addMeasure")
	public String measure(Model model) {
		model.addAttribute("oneMenu", "newTask");
		model.addAttribute("twoMenu", "measure");
		return "/cms/newTask/addMeasure";
	}

	// 查看我的订单
	@RequestMapping("/measureList/{status}")
	public String measureList(Model model, @PathVariable("status") String status) {
		logger.debug("page的状态是：{}", status);
		model.addAttribute("oneMenu", "taskList");
		model.addAttribute("twoMenu", "measureList");
		if ("pay".equals(status)) {
			return "/cms/taskList/measureList0";
		} else if ("taking".equals(status)) {
			System.out.println("测量》状态----------"+status);
			return "/cms/taskList/measureList1";
		} else if ("order".equals(status)) {
			return "/cms/taskList/measureList2";
		} else if ("move".equals(status)) {
			return "/cms/taskList/measureList3";
		} else {
			return "/cms/taskList/measureList4";
		}
	}
	/**
	 * 功能：一键转安装
	 * @param request
	 * @param mesureIndentId
	 * @return
	 */
	@RequestMapping("/toInstallIndent")
	@ResponseBody
	public Map<String,Object> toInstallIndent(HttpServletRequest request,HttpSession session,Integer mesureIndentId){
		System.out.println("……………………………………………………进入一键转安装啦。");
		return measureIndentSER.toInstallIndent(request,session,mesureIndentId);
	}
	
//	/**
//	 * 功能：取消订单
//	 * @param request
//	 * @param mesureIndentId
//	 * @return
//	 */
//	@RequestMapping("/quitMesureIndent")
//	@ResponseBody
//	public Map<String,Object> quitMesureIndent(HttpServletRequest request,HttpSession session,Integer mesureIndentId){
//		return measureIndentSER.quitIndent(request, session, 0, mesureIndentId);
//	}

	/**
	 * 生成测量订单信息
	 * 
	 * @param request
	 * @param phone
	 * @param name
	 * @param roomNumber
	 * @param timestamp
	 * @param measureAddress
	 * @param province
	 * @param city
	 * @param country
	 * @param subscribeTime
	 * @param remark
	 * @param session
	 * @return
	 */
	@RequestMapping("/makeMersureIndent")
	public @ResponseBody Result makeMersureIndent(HttpServletRequest request, String phone, String name,
			Integer roomNumber, String measureAddress, String province, String city, String country,
			String subscribeTime, String remark, HttpSession session) {
		Result result = new Result();
		logger.debug("要添加的订单的详细信息：手机：{}，名称：{}，房间数量：{}，时间戳：{}，详细的地址：{}，省份：{}，城市：{}，城镇：{}，时间：{}，选用了余额宝：{}", phone, name,
				roomNumber, measureAddress, province, city, country, subscribeTime);
		try {
			Integer indentID = measureIndentSER.insertMeasureIndent(request, phone, name, roomNumber, measureAddress,
					province, city, country, subscribeTime, remark, session);
			result.setStatus(0);
			result.setData(indentID);
			Thread.sleep(2000);
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(1);
		}
		return result;
	}

	/**
	 * 进行测量订单的付款
	 * 
	 * @param request
	 * @param indentID
	 * @param addPrice
	 * @param relievedBao
	 * @param session
	 * @return
	 */
	@RequestMapping("/payMeasureIndent")
	public String payMeasureIndent(HttpServletRequest request, Integer indentID, Double addPrice, Integer relievedBao,
			HttpSession session) {
		logger.debug("需要付款的订单ID是：{}，加价费用是：{}，安心保选择：{}", indentID, addPrice, relievedBao);
		try {
			System.out.println("进入测量支付页面--------------");
			System.out.println("订单ID是："+ indentID+",addPrice="+addPrice+",relievedBao="+ relievedBao);
			measureIndentSER.modifyForPayMeasureIndent(request, indentID, addPrice, relievedBao, session);
			return "forward:/cms/alipay/alipayapi.jsp";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据登录的用户查询测量订单
	 * 
	 * @param session
	 * @param cuName
	 * @param cuPhone
	 * @param indentID
	 * @param subscribeTimeBegin
	 * @param subscribeTimeEnd
	 * @param beginTimeBegin
	 * @param beginTimeEnd
	 * @param indentStatus
	 * @param offset
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@RequestMapping("/queryMeasureIndentByMerchant")
	public @ResponseBody Map<String, Object> queryMeasureIndentByMerchant(HttpSession session, String cuName,
			String cuPhone, String indentID, String subscribeTimeBegin, String subscribeTimeEnd, String beginTimeBegin,
			String beginTimeEnd, Integer indentStatus, Integer offset, Integer limit, String sort, String order) {
		Map<String, Object> retMap = null;
		try {
			retMap = measureIndentSER.selectMeasureIndentByMerchant(session, cuName, cuPhone, indentID,
					subscribeTimeBegin, subscribeTimeEnd, beginTimeBegin, beginTimeEnd, indentStatus, offset, limit,
					sort, order);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retMap;
	}
}
